package com.suda.sudaexamsystem.controller;

import com.suda.sudaexamsystem.dto.ExamPaperDto;
import com.suda.sudaexamsystem.dto.QuestionDto;
import com.suda.sudaexamsystem.pojo.ExamPaper;
import com.suda.sudaexamsystem.pojo.Question;
import com.suda.sudaexamsystem.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class ExamHtmlController {
    @Autowired
    private ExamPaperService examPaperService;

    @GetMapping(value = "/search/{examId}")
    public String findExamPaper(@PathVariable("examId") int examId, Model model) {
        ExamPaper examPaper = examPaperService.findById(examId);
        ExamPaperDto examPaperDto = new ExamPaperDto();
        examPaperDto.setClazz(examPaper.getClazz().toDto());
        examPaperDto.getClazz().setOwner(examPaper.getClazz().getOwner().toDto());
        examPaperDto.setId(examPaper.getId());
        examPaperDto.setName(examPaper.getName());
        examPaperDto.setScore(examPaper.getScore());
        List<QuestionDto> questionDtoList = examPaperDto.getQuestionDtoList();
        for (Question question : examPaper.getQuestionList()) {
            QuestionDto questionDto = new QuestionDto();
            questionDto.setId(question.getId());
            questionDto.setName(question.getName());
            questionDto.setScore(question.getScore());
            questionDto.setType(question.getType());
            questionDto.setAnswer(question.getAnswer());
            questionDto.setOptionA(question.getOptionA());
            questionDto.setOptionB(question.getOptionB());
            questionDto.setOptionC(question.getOptionC());
            questionDto.setOptionD(question.getOptionD());
            questionDto.setOrderId(question.getOrderId());
            questionDtoList.add(questionDto);
        }
        model.addAttribute("examPaper", examPaperDto);
        return "user/examDetail";
    }
}
